package com.ruijie.acl.web;
import java.util.ArrayList;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.jsf.ListDataModel;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.AclDevice;
import com.ruijie.acl.AclDeviceAccessGroup;
import com.ruijie.acl.service.AccessGroupService;
import com.ruijie.acl.service.DeviceAccessGroupService;
import com.ruijie.emp.device.Device;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.foundation.service.Repository;

/**
 * <p>
 * Title:在ACL组详细页面中管理ACL设备的ACTION

 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 
 * </p>
 * <p>
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * @author ASAI
 */
@Name("accessGroupManageInDeviceAction")
@Scope(ScopeType.CONVERSATION)
public class AccessGroupManageInDeviceAction extends AclPlanBaseAction{

	@In("#{deviceAccessGroupService}")
	private DeviceAccessGroupService deviceAccessGroupService;
	
	@In("#{accessGroupService}")
	private AccessGroupService accessGroupService;
	@In("#{accessGroupAction}")
	private AccessGroupAction accessGroupAction;
	
	
	private ListDataModel accessGroupEntityModelList = null;
	
	//ACL设备信息
	private AclDevice aclDevice;
	private Long deviceId = null;
	
	
	/**
	 * 在设备详细中，取得ACL组分页信息
	 * @return 组列表
	 */
	@SuppressWarnings("unchecked")
	public ListDataModel getAccessGroupEntityModelList(AclDevice aclDevice) {
		if(aclDevice != null) {
			this.aclDevice = aclDevice;
			//if(accessGroupEntityModelList == null || aclDevice.getId() != this.deviceId) {
			adjustPage();
			currentPage = deviceAccessGroupService.pageQueryByAclDevice(
						aclDevice,null, page, pageSize);
			accessGroupEntityModelList = new ListDataModel(
						(List<AclDeviceAccessGroup>) currentPage.getResult());
			//}
			this.deviceId = aclDevice.getId();
		}
		return accessGroupEntityModelList;
	}
	
	/**
	 * 删除设备组之间的关联
	 * @param entity
	 */
	public void delete(AclDeviceAccessGroup entity) {
		deviceAccessGroupService.remove(entity);
		AccessGroup accessGroupDevice = accessGroupService.getByNameWithAclDevice(entity.getAccessGroup().getName(), entity.getAclDevice());
		if (accessGroupDevice != null){
			accessGroupService.remove(accessGroupDevice);
		}
		updateModel();
	}
	
	@Override
	protected void updateModel() {
		this.accessGroupEntityModelList = null;
		getAccessGroupEntityModelList(aclDevice);
	}
	
	/**
	 * 根据ACL设备取得ACL组列表
	 * @param entity ACL设备
	 * @return ACL组列表
	 */
	public List<AccessGroup> getAccessGroupByAclDevice(AclDevice entity){
		List<AclDeviceAccessGroup> list = this.deviceAccessGroupService.getByAclDevice(entity);
		List<AccessGroup> accessGroupList = new ArrayList<AccessGroup>();
		if (list != null && list.size() > 0){
			for(AclDeviceAccessGroup aclDeviceAccessGroup : list){
				accessGroupList.add(aclDeviceAccessGroup.getAccessGroup());
			}
		}
		return accessGroupList;
	}
	
	/**
	 * ACL组的单设备部署
	 * @param id
	 * @param device
	 */
	public void reConfig(String id,Device device){		
		AccessGroup group = null;
		try {
			group = accessGroupService.get(Long.valueOf(id));
			accessGroupService.reConfig(group, device, "modify");
		} catch (BusinessException e) {
			throw e;
		}
		accessGroupAction.resetNewestModel();
		updateModel();
		addGlobalI18nMessage("acl_device_deploy_success");
	}
	
	/**
	 * ACL组详细界面的删除设备

	 * @param id
	 * @param device
	 */
	public void deleteDeviceConfig(String id,Device device,AclDeviceAccessGroup entity){		
		AccessGroup group = null;
		try {
			group = accessGroupService.get(Long.valueOf(id));
			accessGroupService.reConfig(group, device, "DeleteAccessGroup");
		} catch (BusinessException e) {
			throw e;
		}
		entity = deviceAccessGroupService.get(entity.getId());
		this.delete(entity);
		accessGroupService.removeAclInterface(group,device);
		accessGroupAction.resetNewestModel();
		updateModel();
		addGlobalI18nMessage("crud_del_success_msg");
	}
	
	
	
	

	public ListDataModel getAccessGroupEntityModelList() {
		return accessGroupEntityModelList;
	}

	public void setAccessGroupEntityModelList(
			ListDataModel accessGroupEntityModelList) {
		this.accessGroupEntityModelList = accessGroupEntityModelList;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	protected Repository getRepository() {
		return deviceAccessGroupService;
	}

	@Override
	protected String getAddPage() {
		return null;
	}

	@Override
	public EntityObject getCriteria() {
		return null;
	}

	@Override
	protected String getDetailPage() {
		return null;
	}

	@Override
	protected String getListPage() {
		return null;
	}

	@Override
	public EntityObject getModel() {
		return null;
	}

	@Override
	protected String getUpdatePage() {
		return null;
	}

	@Override
	public void resetModel() {

	}

	@Override
	public void setCriteria(EntityObject criteria) {

	}

	@Override
	public void setModel(EntityObject model) {


	}

	public AclDevice getAclDevice() {
		return aclDevice;
	}

	public void setAclDevice(AclDevice aclDevice) {
		this.aclDevice = aclDevice;
	}

	public Long getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(Long deviceId) {
		this.deviceId = deviceId;
	}
}





















